// 二次封装axios
import axios from 'axios'
import qs from "qs";
import { sign } from "@/components/Dialog/DragVerify";
import Vue from 'vue';
import Router from '@/router'

const service = axios.create({
  baseURL: 'https://hcactapi.yfqwl.com',
  withCredentials: false,
  timeout: 20000
  // httpAgent: new window["http"].Agent({ keepAlive: true }),
});
service.interceptors.request.use(config=> {
  config.headers = {
    'aid': 'cwact',
    'sid': localStorage.getItem('sessionId') || '',
    'actcode': 'ZPAJc'
  }
  const data = config.data || config.params;
  config.data = qs.stringify(data);

  return config;

}, error=> {
  return Promise.reject(error);
})


service.interceptors.response.use(
  resp => {
    // TODO http异常代码处理
    // TODO 接口自定义请求异常处理
    return resp.data;
  },
  error => {
    if (error.code == 'ERR_NETWORK') {
      Vue.prototype.$toast(error.code)
      return Promise.reject(error);
    }

    let status = error.response.status;
    if (status && (status == 601 || status == 604 || status == 605 || status == 500)) {
      // Vue.prototype.$Cookies.remove('sessionId');
      // Vue.prototype.$Cookies.remove('userinfo');
      // Vue.prototype.$Cookies.remove('userinfo');
      localStorage.removeItem('sessionId');
      localStorage.removeItem('userinfo');
      localStorage.removeItem('token');
      localStorage.removeItem('isregister');
      const u = window.location.href;
      Router.replace('/loading')
    }
    return Promise.reject(error);


  }
);

export default service;